package com.example.sls.controller.student;

import com.example.sls.pojo.admin.defaultProSet.DefaultProSet;
import com.example.sls.pojo.student.Student;
import com.example.sls.service.Class.ClassService;
import com.example.sls.service.admin.defaultProSet.DefaultProSetService;
import com.example.sls.service.student.StudentService;
import com.example.sls.service.teacher.pro.ProService;
import com.example.sls.service.teacher.proSet.ProSetService;
import com.github.pagehelper.PageInfo;
import org.jetbrains.annotations.ApiStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassService classService;

    @Autowired
    private ProService proService;

    @Autowired
    private DefaultProSetService defaultProSetService;

    //学生分页获取默认题集信息
    @GetMapping("/getPageInfo4Stu/{pageNum}/{pageSize}")
    public PageInfo<DefaultProSet> getAllPros4Stu(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        return defaultProSetService.getAllPros4StuByPage(pageNum, pageSize);
    }

    //学生通过proType分页获取默认题集信息
    @GetMapping("/getPageInfo4StuByProType/{proType}/{pageNum}/{pageSize}")
    public PageInfo<DefaultProSet> getPageInfo4StuByProType(@PathVariable("proType") Integer proType, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        return defaultProSetService.getPageInfo4StuByProType(proType, pageNum, pageSize);
    }


    //只返回题号和题目内容
    @GetMapping("/getProById/{id}")
    public DefaultProSet getProById(@PathVariable("id") Integer id){
        return defaultProSetService.getProById(id);
    }



    //通过id获取学生所有信息
    @GetMapping("/getStuInfoById/{id}")
    public Student getStudentInfoById(@PathVariable("id") Integer id){
        System.out.println("========");
        System.out.println(id);
        System.out.println("========");
        System.out.println(studentService.getStudentInfoById(id));
        return studentService.getStudentInfoById(id);
    }

    //通过classId分页获取该班级所有学生
    @GetMapping("/getStudentsPageInfo/{classId}/{pageNum}/{pageSize}")
    public PageInfo<Student> getPageInfoByCId(@PathVariable("classId") Integer classId, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        return studentService.getPageInfoByCId(classId, pageNum, pageSize);
    }

    //退出班级(实质上就是将当前学生的classId设为默认值并退出当前页面)
    @PostMapping("/leaveClass/{studentId}")
    public Map<String, String> leaveClass(@PathVariable("studentId") Integer studentId){
        Map<String, String> res = new HashMap<>();

        System.out.println("studentId: " + studentId);

        Integer cId = studentService.getCIdBySId(studentId);  //classId
        System.out.println("classId: " + cId);
        Integer cnt = classService.getCountByCId(cId);   //班级人数
        System.out.println("cnt: " + cnt);

        if(studentService.leaveClass(studentId)){  //classId变为-1
            //退出班级成功后, 原班级人数-1
           // Integer classId = studentService.getCIdBySId(studentId);

           // Integer cnt = classService.getCountByCId(classId);

            if(cnt <= 0){
                if(!classService.setCountByCId(cId, 0 )){
                    res.put("error_message", "数据同步修改出错");
                    return res;
                }
            }
            else{
                if(! classService.setCountByCId(cId, cnt - 1 )){
                    res.put("error_message", "数据同步修改出错");
                    return res;
                }
            }
            res.put("error_message", "success");
        }
        else{
            res.put("error_message", "退出出错");
        }

        return res;
    }


    //后台进行答案判断
    @PostMapping("/judge")
    public  Map<String, String> judgeAnswer(@RequestParam Map<String, String> params){

        Integer id = Integer.valueOf(params.get("id"));    //题号
        String ans = params.get("answer");   //用户的答案

        System.out.println("========");
        System.out.println("id: " + id);
        System.out.println("answer: " + ans);
        System.out.println("========");


        Map<String, String> res = new HashMap<>();

        if(proService.judgeAnswer(id, ans)){
            res.put("error_message", "success");
        }
        else{
            res.put("error_message", "答案错误");
        }

        return res;
    }


}
